package com.hky.front_end_monitor.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hky.front_end_monitor.entity.SysUserVital;
import com.hky.front_end_monitor.service.SysUserVitalService;
import com.hky.front_end_monitor.mapper.SysUserVitalMapper;
import com.hky.front_end_monitor.utils.NumberUtils;
import com.hky.front_end_monitor.utils.StringUtils;
import com.hky.front_end_monitor.utils.UuidGen;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author hello
 * @description 针对表【sys_user_vital】的数据库操作Service实现
 * @createDate 2022-07-31 12:59:24
 */
@Service
public class SysUserVitalServiceImpl extends ServiceImpl<SysUserVitalMapper, SysUserVital>
        implements SysUserVitalService {

    @Resource
    SysHttpRecordServiceImpl sysHttpRecordService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean storeUserVital(Map map) {
        return this.storeUserVital(map, UuidGen.getUUID());
    }

    @Override
    public boolean storeUserVital(Map map, String userVitalUUID) {
        String userId = StringUtils.caseToString(map.get("userId"));
        SysUserVital userVital = new SysUserVital();
        userVital.setUserVitalId(userVitalUUID);
        userVital.setUserId(userId);
        userVital.setTimestamp(NumberUtils.toLong(map.get("timestamp")));
        LinkedHashMap pi = (LinkedHashMap) map.get("pageInfo");
        userVital.setPiHost((String) pi.get("host"));
        userVital.setPiHostname((String) pi.get("hostname"));
        userVital.setPiHref((String) pi.get("href"));
        userVital.setPiProtocol((String) pi.get("protocol"));
        userVital.setPiOrigin((String) pi.get("origin"));
        userVital.setPiPort((String) pi.get("port"));
        userVital.setPiPathname((String) pi.get("pathname"));
        userVital.setPiSearch((String) pi.get("search"));
        userVital.setPiHash((String) pi.get("hash"));
        userVital.setPiTitle((String) pi.get("title"));
        userVital.setPiUserAgent((String) pi.get("userAgent"));
        userVital.setPiLanguage((String) pi.get("language"));
        userVital.setPiWinScreen((String) pi.get("winScreen"));
        userVital.setPiDocScreen((String) pi.get("docScreen"));
        LinkedHashMap oi = (LinkedHashMap) map.getOrDefault("originInformation", map);
        userVital.setReferrer(StringUtils.caseToString(oi.get("referrer")));
        userVital.setType(NumberUtils.toInteger(oi.get("type")));
        return this.save(userVital);
    }
}




